package com.github.sevenmall.orderservice.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.sevenmall.orderservice.dao.SevenmallCartMapper;
import com.github.sevenmall.orderservice.pojo.do0.SevenmallCart;
import com.github.sevenmall.orderservice.pojo.do0.SevenmallCartExample;
import com.github.sevenmall.orderservice.service.CartService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.math.BigDecimal;
import java.time.LocalDateTime;
import java.util.List;

@Service
public class CartServiceImpl implements CartService {
    @Autowired
    private SevenmallCartMapper sevenmallCartMapper;
    @Override
    public SevenmallCart queryExist(Integer goodsId, Integer productId, Integer userId) {
        SevenmallCartExample example = new SevenmallCartExample();
        example.or().andProductIdEqualTo(goodsId).andSkuIdEqualTo(productId).andUserIdEqualTo(userId).andDeletedEqualTo(false);
        return sevenmallCartMapper.selectOneByExample(example);
    }

    @Override
    public int addCart(SevenmallCart sevenCart) {
        sevenCart.setAddTime(LocalDateTime.now());
        sevenCart.setUpdateTime(LocalDateTime.now());
        return sevenmallCartMapper.insert(sevenCart);
    }

    @Override
    public int updateById(SevenmallCart sevenCart) {
        sevenCart.setUpdateTime(LocalDateTime.now());
        return sevenmallCartMapper.updateByPrimaryKey(sevenCart);
    }

    @Override
    public List<SevenmallCart> queryByUid(int userId) {
        SevenmallCartExample example=new SevenmallCartExample();
        example.or().andUserIdEqualTo(userId).andDeletedEqualTo(false);
        return sevenmallCartMapper.selectByExample(example);
    }

    @Override
    public List<SevenmallCart> queryByUidAndChecked(Integer userId) {
        SevenmallCartExample example=new SevenmallCartExample();
        example.or().andUserIdEqualTo(userId).andCheckedEqualTo(true).andDeletedEqualTo(false);
        return sevenmallCartMapper.selectByExample(example);
    }

    @Override
    public int delete(List<Integer> skuIdList, int userId) {
        SevenmallCartExample example=new SevenmallCartExample();
        example.or().andUserIdEqualTo(userId).andSkuIdIn(skuIdList);
        return sevenmallCartMapper.logicalDeleteByExample(example);
    }

    @Override
    public SevenmallCart findById(Integer id) {
        return sevenmallCartMapper.selectByPrimaryKey(id);
    }

    @Override //根据用户id和购物车id 查询没有被删除的购物车的详情
    public SevenmallCart findById(Integer userId, Integer id) {
        SevenmallCartExample example=new SevenmallCartExample();
        example.or().andUserIdEqualTo(userId).andIdEqualTo(id).andDeletedEqualTo(false);
        return sevenmallCartMapper.selectOneByExample(example);
    }

    @Override//用户id,货品id,选择状态
    public int updateCheck(Integer userId, List<Integer> skuList, Boolean checked) {
        SevenmallCartExample example=new SevenmallCartExample();
        example.or().andUserIdEqualTo(userId).andSkuIdIn(skuList).andDeletedEqualTo(false);
        SevenmallCart cart=new SevenmallCart();
        cart.setChecked(checked);
        cart.setUpdateTime(LocalDateTime.now());
        return sevenmallCartMapper.updateByExampleSelective(cart,example);
    }

    @Override
    public void clearGoods(Integer userId) {
        SevenmallCartExample example=new SevenmallCartExample();
        example.or().andUserIdEqualTo(userId).andCheckedEqualTo(true);
        SevenmallCart sevenCart=new SevenmallCart();
        sevenCart.setDeleted(true);
        sevenmallCartMapper.updateByExampleSelective(sevenCart,example);
    }

    @Override  //分页查询
    public List<SevenmallCart> querySelective(Integer userId, Integer goodsId, Integer page, Integer limit, String sort, String order) {
        SevenmallCartExample example = new SevenmallCartExample();
        SevenmallCartExample.Criteria criteria = example.createCriteria();

        if (!StringUtils.isEmpty(userId)) {
            criteria.andUserIdEqualTo(userId);
        }
        if (!StringUtils.isEmpty(goodsId)) {
            criteria.andProductIdEqualTo(goodsId);
        }
        criteria.andDeletedEqualTo(false);

        if (!StringUtils.isEmpty(sort) && !StringUtils.isEmpty(order)) {
            example.setOrderByClause(sort + " " + order);
        }

        PageHelper.startPage(page, limit);
        return sevenmallCartMapper.selectByExample(example);
    }

    @Override
    public int deleteById(Integer id) {
        return sevenmallCartMapper.logicalDeleteByPrimaryKey(id);
    }

    @Override //查看有没有选择这种商品,不是货品
    public boolean checkExist(Integer goodsId) {
        SevenmallCartExample example=new SevenmallCartExample();
        example.or().andProductIdEqualTo(goodsId).andCheckedEqualTo(true).andDeletedEqualTo(false);
        return sevenmallCartMapper.countByExample(example)!=0;
    }

    @Override
    public void updateProduct(Integer id, String goodsName, BigDecimal price, String url) {
         SevenmallCartExample example=new SevenmallCartExample();
         example.or().andProductIdEqualTo(id);
         SevenmallCart cart=new SevenmallCart();
         cart.setPrice(price);
         cart.setPicUrl(url);
         cart.setProductName(goodsName);
         sevenmallCartMapper.updateByExampleSelective(cart,example);
    }
}
